package com.netease.worldcuplive;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.Toast;

import com.avos.avoscloud.AVOSCloud;
import com.netease.worldcuplive.fragment.GameFragment;
import com.netease.worldcuplive.fragment.RankingsFragment;
import com.netease.worldcuplive.fragment.ScheduleFragment;
import com.netease.worldcuplive.fragment.VideoFragment;
import com.netease.worldcuplive.listener.NsFragmentTabHost;
import com.netease.worldcuplive.listener.NsFragmentTabHost.NsFragmentItem;
import com.netease.worldcuplive.utils.NetWorkChecker;

/**
 * @author Atlantis Ho      email: hunterhbb@gmail.com
 * @version 创建时间: 2014年5月7日 上午10:01:38
 * 类说明:
 */
/**
 * 
 * @ClassName: HomeActivity 
 * @Description: 主入口的activity
 * @author xsl
 * @date 2014年6月19日 上午10:46:30 
 *
 */
public class HomeActivity extends FragmentActivity{
	/**
	 * 对应主界面下面四个标签
	 */
	private static final String TAG_VIDEO = "video";//视频标签
	private static final String TAG_GAME = "game";//比赛界面
	private static final String TAG_SCHEDULE = "schedule";//赛程界面
	private static final String TAG_RANKINGS = "rankings";//榜单界面


	//底部四个控件
	View v_video;//视频界面
	View v_game;//比赛控件
	View v_schedule;//赛程
	View v_rankings;//榜单

	private NsFragmentTabHost nsTabHost;
	private FragmentTabHost tabHost;

	//	private static TextView home_title;

	//	private Button settings;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		//		requestWindowFeature(Window.FEATURE_NO_TITLE);
		//测试用key
		//		AVOSCloud.initialize(this, "o1x1krpc8bj0zzs7ty3ckspsnjmstf39yejw2ku9iw1s484i", "ryb6zbtl26pk7kf7hwra73ddbxtcevp7vc722odqvdlk8ja0");
		//链接服务器
		initCloudService();


		setContentView(R.layout.activity_home);
		//初始化主界面
		initHomeView();


		if( ! NetWorkChecker.isNetWorkOpen(this)){
			Toast.makeText(this, R.string.please_open_network, Toast.LENGTH_SHORT).show();
		}
	}












	/**
	 * 
	 * @Title: initCloudService 
	 * @Description: 初始化服务器连接 
	 * @param     设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void initCloudService() {
		AVOSCloud.initialize(this, "vihv8ophnqy74k5tg1s1asbilkox1ydk3cx7ifjqlhkutnjp", "kqd9jopm5b4rb5pglq3makbh6p7iygpdr1ncnmyojedimos0");
	}













	private void initHomeView() {
		tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);



		nsTabHost = new NsFragmentTabHost(tabHost, this, R.id.realtabcontent);
		//初始化底部四个标签
		initTabBottomView();
		//初始化视频标签和对应的fragment
		NsFragmentItem item_video = InitVideoFragment();
		//初始化比赛标签
		NsFragmentItem item_game = initGrameFragment();
		//初始化赛程标签
		NsFragmentItem item_schedule = initScheduleFragment();
		//初始化榜单标签
		NsFragmentItem item_rankings = initRankingFragment();
		//给TabHost添加上4个子fragment
		nsAddBottomIndictorIndictorFragment(item_video, item_game,
				item_schedule, item_rankings);
	}













	private void nsAddBottomIndictorIndictorFragment(NsFragmentItem item_video,
			NsFragmentItem item_game, NsFragmentItem item_schedule,
			NsFragmentItem item_rankings) {
		nsTabHost.addTab(item_video);
		nsTabHost.addTab(item_game);
		nsTabHost.addTab(item_schedule);
		nsTabHost.addTab(item_rankings);
	}
	private NsFragmentItem initRankingFragment() {
		NsFragmentItem item_rankings = new NsFragmentItem();
		item_rankings.view = v_rankings;
		item_rankings.tag =TAG_RANKINGS;
		item_rankings.fragment = RankingsFragment.class;
		item_rankings.downStyle = R.drawable.icon_nav_board_actived;
		item_rankings.upStyle = R.drawable.icon_nav_board;
		return item_rankings;
	}
	private NsFragmentItem initScheduleFragment() {
		NsFragmentItem item_schedule = new NsFragmentItem();
		item_schedule.view = v_schedule;
		item_schedule.tag = TAG_SCHEDULE;
		item_schedule.fragment = ScheduleFragment.class;
		item_schedule.downStyle = R.drawable.icon_nav_schedule_actived;
		item_schedule.upStyle = R.drawable.icon_nav_schedule;
		return item_schedule;
	}
	/**
	 * 
	 * @Title: initGrameFragment 
	 * @Description: 初始化比赛标签 
	 * @param @return    设定文件 
	 * @return NsFragmentItem    返回类型 
	 * @throws
	 */
	private NsFragmentItem initGrameFragment() {
		NsFragmentItem item_game = new NsFragmentItem();
		item_game.view = v_game;
		item_game.tag = TAG_GAME;
		item_game.fragment = GameFragment.class;
		item_game.downStyle = R.drawable.icon_nav_match_actived;
		item_game.upStyle = R.drawable.icon_nav_match;
		return item_game;
	}
	/**
	 * 
	 * @Title: InitVideoFragment 
	 * @Description: 初始化视频标签和对应的fragment
	 * @param @return    设定文件 
	 * @return NsFragmentItem    返回类型 
	 * @throws
	 */
	private NsFragmentItem InitVideoFragment() {
		NsFragmentItem item_video = new NsFragmentItem();
		item_video.view = v_video;
		item_video.tag = TAG_VIDEO;
		item_video.fragment = VideoFragment.class;
		item_video.downStyle = R.drawable.icon_nav_video_actived;
		item_video.upStyle = R.drawable.icon_nav_video;
		return item_video;
	}
	/**
	 * 
	 * @Title: initTabBottomView 
	 * @Description:初始化底部四个标签
	 * @param     设定文件 
	 * @return void    返回类型 
	 * @throws
	 */
	private void initTabBottomView() {
		v_video = nsTabHost.newTabView(R.layout.home_tab,  "视频", R.drawable.icon_nav_video);
		v_game = nsTabHost.newTabView(R.layout.home_tab,  "比赛", R.drawable.icon_nav_match);
		v_schedule = nsTabHost.newTabView(R.layout.home_tab,  "赛程", R.drawable.icon_nav_schedule);
		v_rankings = nsTabHost.newTabView(R.layout.home_tab,  "榜单", R.drawable.icon_nav_board);
	}

}
